package gui;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.TitledBorder;

public class PanelSorteador extends JPanel implements ActionListener {

	
	//eventos
	
	public final static String PROCESAR = "procesar";	
	
	//elementos graficos
	
	private JLabel labInsert;
	private JLabel labIn;
	private JLabel labOut;
	
	private JButton btnPush;
	
	private JTextField txtIn;
	private JTextField txtOut;
	
	//contacto con GUI principal
	
	GUISorter interfase ;
	
	//datos
	
	private String[] arreglo;
	
	public PanelSorteador(GUISorter inter){
		
		interfase=inter;
		
		//inicializar el panel
		
		setLayout(new GridLayout(3,2));
		//setBackground(Color.GRAY);
		setPreferredSize(new Dimension(0,68));
		
		TitledBorder borde= BorderFactory.createTitledBorder("Array Test");
		borde.setTitleColor(Color.BLUE);
		setBorder(borde);
		
		//init los elementos
		
		labInsert= new JLabel("Inserte el arreglo: ");
		labIn=new JLabel("ex: 3 2 1: ");
		labOut=new JLabel("Output: ");
		
		btnPush= new JButton("Push");
		btnPush.setActionCommand(PROCESAR);
		btnPush.addActionListener(this);
		
		txtIn=new JTextField();
		txtOut= new JTextField();
		
		//aniadir los elementos
		
		add(labInsert);
		add(btnPush);
		add(labIn);
		add(txtIn);
		add(labOut);
		add(txtOut);
		
	}

	
	public String[] darArreglo(){

		return arreglo;
	}
	

	@Override
	public void actionPerformed(ActionEvent evento) {
		
		String com= evento.getActionCommand();
		
		if(com.equals(PROCESAR)){
		
			String temp=txtIn.getText();
			arreglo=temp.split("\\s");
			
			interfase.procesar();
		}
		
	}
	
	public void refresco(String [] arreglo)
	{
		String temp="";
		
		for(int i=0; i < arreglo.length;i++)
		{
			 temp= temp + " " + arreglo[i];
		}
		
		txtOut.setText(temp);
	}
	
	
}
